package site.frame.ftl;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.apache.struts2.views.freemarker.FreemarkerManager;

import javax.servlet.ServletContext;

/**
 * Freemarker 管理器。
 */
public class FtlManager extends FreemarkerManager {

    @Override
    protected Configuration createConfiguration(ServletContext servletContext) throws TemplateException {
        Configuration config = super.createConfiguration(servletContext);

        // 设置freemarker对异常的处理为不处理
        config.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);

        BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
        wrapper.setExposureLevel(BeansWrapper.EXPOSE_ALL);

        config.setSharedVariable("e", wrapper.getStaticModels().get(FtlFunctions.class.getName()));

        config.setSharedVariable("enums", wrapper.getEnumModels());
        config.setSharedVariable("statics", wrapper.getStaticModels());
        config.setClassicCompatible(true);
        config.setNumberFormat("#");

        return config;
    }
}